package com.jack.di

import com.jack.repository.HomeArticleRemoteRepo
import com.jack.repository.HomeArticleRepo
import com.jack.repository.HomeArticleTestRepe
import com.jack.service.HomeArticleService
import com.jack.service.HomeArticleServiceImpl
import org.koin.core.component.KoinComponent
import org.koin.core.context.startKoin
import org.koin.core.qualifier.named
import org.koin.dsl.module
import tornadofx.DIContainer
import tornadofx.FX
import kotlin.reflect.KClass

object DIMgr {
    private val repositoryModule = module {
        single<HomeArticleRepo>(named("remote")) { HomeArticleRemoteRepo() }
        single<HomeArticleRepo>(named("test")) { HomeArticleTestRepe() }
    }

    private val serviceModule = module {
        single<HomeArticleService> { HomeArticleServiceImpl(get(named("test"))) }
    }

    fun startInject() {
        startKoin {
            modules(repositoryModule, serviceModule)
        }
        FX.dicontainer = object : DIContainer, KoinComponent {
            override fun <T : Any> getInstance(type: KClass<T>): T = getKoin().get(type)
        }
    }
}